﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Icst.Leaflike.Client;
using Icst.Leaflike.Logging;

namespace Icst.Leaflike.SAFG
{
    public class SAFGClient : IDisposable
    {
        private SearchServiceClient _inner = null;
        private SearchServiceClient Inner
        {
            get
            {
                if (_inner == null)
                {
                    lock (this)
                    {
                        _inner = new SearchServiceClient();
                    }
                }

                return _inner;
            }
        }

        public Plant[] SearchPlants(Contour contour, int maxcount)
        {
            try
            {
                return Inner.SearchPlants(contour, maxcount);
            }
            catch (Exception e)
            {
                Logger.Error("SearchPlants FAILURE. " + e.Message);
                throw;
            }
        }

        public Contour[] SearchContours(byte[] bytes)
        {
            try
            {
                return Inner.SearchContours(bytes);
            }
            catch (Exception e)
            {
                Logger.Error("SearchPlants FAILURE. " + e.Message);
                throw;
            }
        }

        public Contour[] SearchContoursByUrl(string url)
        {
            try
            {
                return Inner.SearchContoursByUrl(url);
            }
            catch (Exception e)
            {
                Logger.Error("SearchContoursByUrl FAILURE. " + e.Message + ". url=" + url);
                throw;
            }
        }
        public Contour[] SearchContoursByPath(string path)
        {
            try
            {
                return Inner.SearchContoursByPath(path);
            }
            catch (Exception e)
            {
                Logger.Error("SearchContoursByPath FAILURE. " + e.Message + ". path=" + path);
                throw;
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (_inner != null)
                {
                    try
                    {
                        _inner.Close();
                    }
                    catch
                    {
                        _inner.Abort();
                    }
                    finally
                    {
                        _inner = null;
                    }
                }
            }
        }
    }
}
